//
//  JMExtenDetailVC.m
//  NewiOSProject
//
//  Created by JMMac on 16.1.24.
//  Copyright © 2024 范智渊. All rights reserved.
//

#import "JMExtenDetailVC.h"
#import "JMOrderInfoModel.h"
#import "JMOrderInfoCell.h"
#import "JMRepayVC.h"
#import "JMOrderHeaderV.h"
@interface JMExtenDetailVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)JMOrderHeaderV *headView;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)UIView *bottomView;
@property (nonatomic, strong)UIView *foldView;
@property (nonatomic, strong)NSMutableArray *dataList;
@property (nonatomic, strong)JMExtenDetailModel *model;
@property (nonatomic, assign)BOOL isFold;

@end

@implementation JMExtenDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.TitleLabel.text = @"Confirmar la prórroga";
    self.TitleLabel.textColor = WhiteColor();
    self.NavImg.backgroundColor = mainSchemeColor();
    [self getDelayDetail];
    [JMCommonMethod uploadStatistics:@"DelayRepaymentConfirm" actionType:@"Enter" parameters:@{@"loanOrderId":self.infoModel.orderNo}];

}

//展期明细
-(void)getDelayDetail{
    
    [self startAnimating: nil];
    NSDictionary *param = @{@"orderNo":self.infoModel.orderNo,@"pageType":self.infoModel.pageType};
    
    [[JMBaseRequest sharedInstance] requestBasePost:BasePostRequestUrl(@"/api/delay/calculate-delay-flexible") params:param success:^(NSDictionary * _Nullable dataDic, NSArray * _Nullable dataArray) {
        [self stopAnimating];
        self.model = [JMExtenDetailModel mj_objectWithKeyValues:dataDic];
        self.model.infoModel = self.infoModel;
        [self.view addSubview:self.tableView];
        [self.view addSubview:self.bottomView];

        [self.tableView reloadData];
    } fail:^(NSString * _Nonnull errorMsg) {
        [self stopAnimating];
    }];
}
//MARK: -UITableViewDataSource UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.model.extensionDetails.count-(_isFold?3:0);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JMOrderInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JMOrderInfoCell" forIndexPath:indexPath];
    cell.selectionStyle = 0;
    if (indexPath.row < self.model.extensionDetails.count) {
        NSDictionary*dict = self.model.extensionDetails[indexPath.row];
        cell.model = dict;
    }
    return cell;
}

-(void)goPaymentVC{
    [JMCommonMethod uploadStatistics:@"DelayRepaymentMethod" actionType:@"Enter" parameters:@{@"loanOrderId":self.infoModel.orderNo}];

    JMRepayVC *vc = JMRepayVC.new;
    vc.extModel = self.model;
    [self.navigationController pushViewController:vc animated:YES];
}
//MARK: -lazyLoad
-(JMOrderHeaderV *)headView{
    if (!_headView) {
        _headView = [[JMOrderHeaderV alloc]initWithFrame:(CGRectMake(0, 0, kAppW(), 1))];
        _headView.extinfoModel = self.model;
    }
    return _headView;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,NAVIGATION_BAR_HEIGHT(), kAppW(), kAppH()) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.bounces = NO;
        _tableView.rowHeight =52;
        _tableView.dataSource = self;
        [_tableView registerClass:[JMOrderInfoCell class] forCellReuseIdentifier:@"JMOrderInfoCell"]; // 注册UITableViewCell
        _tableView.separatorColor = mainSepartorColor();
        _tableView.sectionHeaderTopPadding = 0;
        _tableView.backgroundColor = Hex_Str_COLOR(@"#fffff");
        [_tableView setTableHeaderView:self.headView];
        _tableView.sectionHeaderTopPadding = 0;
        UIButton *btn = [JMControl createButtonWithFrame:(CGRectMake(0, 0, kAppW(), 60)) title:@"Ocultar" titleColor:mainSchemeColor() titleFont:Font(12) backgroundColor:UIColor.clearColor Target:self Action:@selector(showOrHideDetailView:)];
        [btn setImage:ImageNamed(@"ICON／SHANG-xiao 1") forState:(UIControlStateNormal)];
        [btn setImage:ImageNamed(@"ICON／XIA-xiao") forState:(UIControlStateSelected)];
        [btn TiaoZhengButtonWithOffsit:0 TextImageSite:(UIButtonTextLeft)];
        _tableView.tableFooterView = btn;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = false;
        }
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    return _tableView;
}
-(void)showOrHideDetailView:(UIButton *)sender{
    sender.selected = !sender.isSelected;
    _isFold = sender.isSelected;
    [self.tableView reloadData];
}
-(UIView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]initWithFrame:(CGRectMake(0, 0, kAppW(), 10))];
        UIView *contV = [[UIView alloc]initWithFrame:(CGRectMake(0, 16, kAppW(), 10))];
        contV.backgroundColor = WhiteColor();
        [_bottomView addSubview:contV];
        contV.layer.shadowColor = HexAlpha_Str_COLOR(@"#000000", 0.1).CGColor;
        contV.layer.shadowOpacity = 1;
        contV.layer.shadowRadius = 16;
        
        UIButton * goPaymentBtn = [[JMLoadingButton  alloc]initWithFrame:(CGRectMake(24, 20, kAppW()-48, 56))title:@"Confirmar extender plazo" font:mediumFont(17) titleLabelColor:Hex_Str_COLOR(@"#FEFFCC") backgoundColor:mainGradientColor(CGSizeMake(kAppW()-48, 56)) action:@selector(goPaymentVC) target:self];
        [contV addSubview:goPaymentBtn];
        goPaymentBtn.cornerRadius = 28;
        
        [contV addSubview:self.footerView];
        self.footerView.top = goPaymentBtn.bottom+12;
        contV.height = self.footerView.bottom+24+JF_BOTTOM_SPACE();
        _bottomView.height = contV.bottom;
        _bottomView.top = kAppH()-_bottomView.height;
        _tableView.height = _bottomView.top-NAVIGATION_BAR_HEIGHT();

    }
    return _bottomView;
}

@end
